package com.app.yc.ifapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import java.util.List;

/**
 * 作者：TanTun
 * 时间：2017/3/16
 * 邮箱：32965926@qq.com
 * 描述：适配器
 */

public class MyBaseExpandableListAdapter<TGroup extends IExpandableGroup<TChild>, TChild>
        extends BaseExpandableListAdapter {
    private static final String TAG = "MyBaseExpandableListAdapter";
    private List<TGroup> mContentList;
    private LayoutInflater mInflater;
    private int mGroupId;
    private int mChildId;
    private MyBaseExpandableListAdapter.Callback<TGroup, TChild> mCallback;

    public interface Callback<T1, T2> {
        void getGroupView(int i, View v, T1 group);
        void getChildView(int i, View v, T1 group, T2 child);
    }

    public MyBaseExpandableListAdapter(Context context,
                                       int groupId,
                                       int childId,
                                       List<TGroup> data,
                                       MyBaseExpandableListAdapter.Callback<TGroup, TChild> callback){
        mContentList = data;
        mInflater = LayoutInflater.from(context);
        mGroupId = groupId;
        mChildId = childId;
        mCallback = callback;
    }

    @Override
    public int getGroupCount() {
        return mContentList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return mContentList.get(i).getChild().size();
    }

    @Override
    public Object getGroup(int i) {
        return mContentList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return mContentList.get(i).getChild().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        try {
            if (view == null) view = mInflater.inflate(mGroupId, null);
            mCallback.getGroupView(i, view, mContentList.get(i));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        try {
            if (view == null) view = mInflater.inflate(mChildId, null);
            mCallback.getChildView(i, view, mContentList.get(i),
                    mContentList.get(i).getChild().get(i1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}
